// 分类模块
import {findAllCategory} from "@/api/category";
import {topCategory} from "@/api/constants";

export default {
    namespaced: true,
    state() {
        return {
            // 分类信息集合
            list: topCategory.map(item => ({name: item})),

        }
    },
    mutations: {
        setList(state, headCategory) {
            state.list = headCategory
        },
        // 修改当前一级分类下的open数据为true
        show (state, item) {
            const category = state.list.find(category => category.id === item.id)
            category.open = true
        },
        // 修改当前一级分类下的open数据为false
        hide (state, item) {
            const category = state.list.find(category => category.id === item.id)
            category.open = false
        }
    },
    actions: {
        async getList({commit}) {
            const {result} = await findAllCategory()
            result.forEach(item => {
                item.open = false
            })
            commit('setList', result)
        }
    }
}